<?php

class Comment_AdminController extends Ac_AdminController
{
	
	public function menuAction()
    {
        $this->view->index = $this->_getParam('index');
    }
    
	public function indexAction() 
    {
        $this->listAction();
    }
    
    public function listAction() 
    {
    	$per_page = 10;
        $page      = !empty($_GET['page']) ? $_GET['page'] : 1;
        $this->view->per_page = $per_page;
        $this->view->listing_number = Model_Comment::all()->count();
        $this->view->page = $page;
		
        $tt = array();
        if(!empty($_GET['t'])){
        	if($_GET['t'] === 'today'){
        		$startdate = new MongoDate(strtotime(date('Y-m-d', time()). ' 00:00:00'));
        		$enddate = new MongoDate(strtotime(date('Y-m-d', time()). ' 23:59:59'));
        	}
        	else if($_GET['t'] === 'yesterday'){
        		$startdate = new MongoDate(strtotime(date('Y-m-d', (time()-24*60*60)). ' 00:00:00'));
        		$enddate = new MongoDate(strtotime(date('Y-m-d', (time()-24*60*60)). ' 23:59:59'));
        	}
        	else{
        		$startdate = new MongoDate(strtotime(date('Y-m-d', (time()-7*24*60*60)). ' 00:00:00'));
        		$enddate = new MongoDate(strtotime(date('Y-m-d', (time())). ' 23:59:59'));
        	}
        	$tt['c_dt'] = array('$gt'=>$startdate, '$lt'=>$enddate);
        }
        if(!empty($_GET['search'])){
        	$term = new MongoRegex('/.*'.$_GET['search'].'.*/');
        	$tt['co'] = $term;
        }
        if(!empty($_GET['username']) && empty($_GET['email'])){
        	$arr = array();
        	$t_username = new MongoRegex('/.*'.$_GET['username'].'.*/');
        	foreach (Model_User::all(array('un'=>$t_username)) as $val){
        		$arr[] = $val->_id;
        	}
        	$tt['us'] = array('$in'=>$arr);
        }
        if(!empty($_GET['email']) && empty($_GET['username'])){
        	$arr = array();
        	$t_email = new MongoRegex('/.*'.$_GET['email'].'.*/');
        	foreach (Model_User::all(array('em'=>$t_email)) as $val_e){
        		$arr[] = $val_e->_id;
        	}
        	$tt['us'] = array('$in'=>$arr);
        }
        if(!empty($_GET['username']) && !empty($_GET['email'])){
        	$arr_u = array();
        	$arr_e = array();
        	$t_username = new MongoRegex('/.*'.$_GET['username'].'.*/');
        	foreach (Model_User::all(array('un'=>$t_username)) as $val){
        		$arr_u[] = $val->_id;
        	}
        	$t_email = new MongoRegex('/.*'.$_GET['email'].'.*/');
        	foreach (Model_User::all(array('em'=>$t_email)) as $val_e){
        		$arr_e[] = $val_e->_id;
        	}
        	$arr = array_intersect($arr_u, $arr_e);
        	$tt['us'] = array('$in'=>$arr);
        }
        
                
        if ($page>1) {
            $skip_page = ($_GET['page']-1) * $per_page;
        }

       	if ($page>1) {
            $this->view->list           = Model_Comment::all($tt)->sort(array('c_dt'=>-1))->skip($skip_page)->limit($per_page);
        }
        else {
        	$this->view->list           = Model_Comment::all($tt)->sort(array('c_dt'=>-1))->limit($per_page);
        }
        $this->view->listing_number = $this->view->list->count();
        
        $this->render('list');
    }
    
    public function showcomplainAction()
    {
    	$per_page = 10;
        $page      = !empty($_GET['page']) ? $_GET['page'] : 1;
        $this->view->per_page = $per_page;
        $this->view->listing_number = Model_Complain::all()->count();
        $this->view->page = $page;
        
        if ($page>1) {
            $skip_page = ($_GET['page']-1) * $per_page;
        }
        
    	
    	$this->view->comment = Model_Comment::find($_GET['id']);
    	if ($page>1) {
    		$this->view->complain = Model_Complain::all(array('co_i'=>new MongoId($_GET['id'])))->skip($skip_page)->limit($per_page);
    	}
    	else{
    		$this->view->complain = Model_Complain::all(array('co_i'=>new MongoId($_GET['id'])))->limit($per_page);;
    	}
    	$this->view->listing_number = $this->view->complain->count();
    	$this->render('showcomplain');
    }
    
    public function displayAction()
    {
    	$comment = Model_Comment::find($_GET['id']);
    	$this->view->comment = $comment;
    	$this->render('display');
    }
    
    public function changestatusAction()
    {
    	$comment = Model_Comment::find($_GET['id']);
    	$comment -> status = $_GET['status'];
    	$comment -> save();
    	$this->_redirect($this->view->geturl('list','comment', null, 'admin1'));
    }
    
    public function editAction()
    {
    	if($this->_request->isPost() && $this->_request->isXmlHttpRequest()){
    		$arr = array();
    		$arr['status'] = '';
    		$post = $this->_request->getPost();
    		$id = $post['id'];
    		$content = $post['content'];
    		$comment = Model_Comment::find($id);
    		$content = str_replace("\r\n","<br />",strip_tags($content));
    		$comment -> content = $content;
    		$comment -> status = 'edited';
    		if($comment -> save()){
    			$arr['status'] = 'success';
    		}
    		$this->_helper->json($arr);
    	}else{
    		$this->_redirect('/');
    	}
    }
    
    public function suspiciousAction()
    {
    	$comment = Model_Comment::find($_GET['id']);
    	$this->view->comment = $comment;
    	$this->render('suspicious');
    }
}